package Test;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import interfaces.Pointer;

import org.junit.Test;

import Pointers.SnesPointer;
import Script.Offset;

public class SnesPointerTest {
	
	@Test
	public void onlyGivesLastTwoByteValues() {
		Pointer snesPointer = new SnesPointer(
					0, new Offset(Long.valueOf("1D3412", 16)), new Offset(0));
		
		// Should only return 1234.
		assertEquals(snesPointer.hexValue(), "1234");
	}
	
	@Test
	public void givesLittleEndianBytesasPrintValues() {
		Pointer snesPointer = new SnesPointer(
				0, new Offset(Long.valueOf("1D3412", 16)), new Offset(0));
		Offset off = new Offset(Long.valueOf("3412", 16));
		
		// Should return 3412
		assertTrue(Arrays.equals(off.getByteValue(2), snesPointer.printValue()));
	}
	
	@Test
	public void givesCorrectHexValue() {
		Pointer snesPointer = new SnesPointer(
				0, new Offset(Long.valueOf("1D3412", 16)), new Offset(0));
		assertEquals(snesPointer.hexValue(), "1234");
	}
	
	@Test
	public void prefacesZeroesToUnevenHexValues() {
		Pointer snesPointer = new SnesPointer(
				0, new Offset(Long.valueOf("3402", 16)), new Offset(0));
		assertEquals(snesPointer.hexValue(), "0234");
	}
	
	@Test
	public void returnCorrectPosition() {
		Pointer snesPointer = new SnesPointer(
				123, new Offset(Long.valueOf("0234", 16)), new Offset(0100));
		assertEquals(snesPointer.getPosition(), new Long(123));
	}
	
	@Test
	public void returnsCorrectFileOffset() {
		Pointer snesPointer = new SnesPointer(
				123, new Offset(Long.valueOf("0234", 16)), new Offset(1000000000));
		Offset off = snesPointer.getFileOffset();
		
		assertEquals(off.getValue(), 1000000000);
	}
	
	@Test
	public void sortsPointerCorrectly() {
		Pointer p1 = new SnesPointer(1, new Offset(1003), new Offset(0));
		Pointer p2 = new SnesPointer(2, new Offset(1002), new Offset(0));
		Pointer p2_2 = new SnesPointer(3, new Offset(1002), new Offset(0));
		Pointer p3 = new SnesPointer(4, new Offset(1001), new Offset(0));
		ArrayList<Pointer> pl = new ArrayList<Pointer>();
		
		pl.add(p1);
		pl.add(p2);
		pl.add(p2_2);
		pl.add(p3);
		
		Collections.sort(pl);
		
		assertEquals(pl.get(0).getPosition(), new Long(4));
		assertEquals(pl.get(1).getPosition(), new Long(2));
		assertEquals(pl.get(2).getPosition(), new Long(3));
		assertEquals(pl.get(3).getPosition(), new Long(1));
	}
}
